package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.enumeration.context.BaseContext;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // 对前端传来的密码进行md5加密处理，将明文转成密文
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /*
     *
     * 新增员工
     *
     * */
    @Override
    public void insertEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //由DTO拷贝到实体对象，前提属性名一致
        BeanUtils.copyProperties(employeeDTO, employee);
        //设置状态
        employee.setStatus(StatusConstant.ENABLE);
        //设置密码,md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employeeMapper.insertEmployee(employee);


    }


    /*
     *
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     *
     * */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        //
        /*
         * 分析查询底层其实就是mapper层对数据库操作:
         * 1.统计数据总数  返回值:Integer
         * (可以自己在mapper中定义SQL语句，也可以使用PageHelper实体对象的getTotal方法获取)
         * 2.分页展示员工数据(默认是第一页索引:0，页面记录数：10)  返回值：列表集合
         * */

        /*
         * 开始分页查询
         * PageHelper.startPage底层是基于ThreadLocal获取本地LOCAL_PAGE，解析查询请求获取我们EmployeePageQueryDTO对象中的参数
         * */
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);//Page集合(底层继承了ArrayList)
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        //将Page对象转变为我们所需的List对象
        List<Employee> list = page.getResult();
        long total = page.getTotal();

        //封装进pageResult


        return new PageResult(total, list);
    }

    /*
     *
     *启用或禁用员工账号
     *
     * */
    @Override
    public void updateStatus(Long id, Integer status) {
        Employee employee = Employee.builder().id(id).status(status).build();
        employeeMapper.update(employee);
    }
    /*
     *
     * 根据id查询员工
     *
     * */
    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        //设置密码不可见
        employee.setPassword("****");
        return employee;
    }

    /*
     *
     * 编辑员工数据
     *
     * */
    @Override
    public void updateData(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //将传入的employeeDTO的数据复制到employee
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.update(employee);
    }

}
